home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: cwi.nl!dik
- From: dik@cwi.nl (Dik T. Winter)
- Subject: Re: HELP: Floating point precision
- Message-ID: <Dnz5Lw.KJ3@cwi.nl>
- Sender: news@cwi.nl (The Daily Dross)
- Nntp-Posting-Host: chrysant.cwi.nl
- Organization: CWI, Amsterdam
- References: <3140831B.1597@interport.com>
- Date: Sat, 9 Mar 1996 00:34:44 GMT
-
- In article <3140831B.1597@interport.com> jeremy <jeremy@interport.com> writes:
- > The mechanism for casting 4-byte float to 8-byte double is
- > causing me some problems.
-
- Your problem is not with the cast but with understanding floating point
- arithmetic.
- >
- > float f;
- > double d;
- > f = 1.332;
- > d = (double) f;
- > results in:
- > f = 1.33200
- > d = 1.3320000171661
-
- A float is in general not able to represent all *decimal* numbers
- exactly. A float is in binary. For instance 1.332 is one of those
- numbers that have no exact representation in binary. The closest
- binary number with 24 bits of precision (as your float probably has)
- is 1.3320000171661376953125 (and this one is exact). So the cast
- correctly converts the float f to a double. Try to print f with
- more digits of precision and see what happens.
- --
- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924098
- home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
-